package org.microservice.system.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.microservice.common.bean.MenuVo;
import org.microservice.common.bean.UserVo;
import org.microservice.common.util.AuthUtil;
import org.microservice.common.util.R;
import org.microservice.common.util.TreeUtil;
import org.microservice.system.dao.SysMenuDao;
import org.microservice.system.entity.SysRoleMenuEntity;
import org.microservice.system.service.SysMenuService;
import org.microservice.system.service.SysRoleMenuService;
import org.microservice.system.service.SysUserService;
import org.microservice.system.utils.dto.MenuTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 齐丹枫
 * Date: 2018/6/20
 * Time: 15:53
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao,MenuVo> implements SysMenuService{

    @Autowired
    private SysMenuDao sysMenuDao;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysUserService sysUserService;

    @Override
    public List<MenuTree> getMenuTreeByUserNameAndMenuType(String userName,String[] menuType,boolean isTree) {

        List<MenuVo> menus = sysMenuDao.getMenuTreeByUserNameAndMenuType(userName, menuType);

        List<MenuTree> menuTreeList = new ArrayList<>();
        menus.forEach(menuVo -> {
                menuTreeList.add(new MenuTree(menuVo));
        });

        if(!isTree){
            return  menuTreeList;
        }
        return TreeUtil.bulid(menuTreeList, 0);
    }

    @Override
    public List<MenuVo> getMenusByRoleId(int roleId) {
     return sysMenuDao.getMenusByRoleId(roleId);
    }

    @Override
    public List<MenuTree> getMenuListPage(Map<String, Object> params) {
        List<MenuVo> menuVos = sysMenuDao.getMenuListPage(params);
        List<MenuTree> menuTreeList = new ArrayList<>();
        menuVos.forEach(menuVo -> {
            menuTreeList.add(new MenuTree(menuVo));
        });
        ;

        return TreeUtil.bulid(menuTreeList, params.get("type").equals("")?0:params.get("type"));
    }

    @Override
    public int getMenuListPageTotal(Map<String, String> params) {
        return sysMenuDao.getMenuListPageTotal(params);
    }

    @Override
    public R save(MenuVo menuVo) {
        sysMenuDao.insert(menuVo);
        String userName = AuthUtil.getUserName();
        UserVo userVo = sysUserService.getUserInfoByUserName(userName);
        SysRoleMenuEntity sysRoleMenuEntity = new SysRoleMenuEntity(userVo.getRoles().get(0).getId(), menuVo.getId());
        sysRoleMenuService.insert(sysRoleMenuEntity);
        return R.ok();
    }

    @Override
    public void disabledOrEnabledById(int id, boolean disabled) {
        sysMenuDao.disabledOrEnabledById(id,disabled);
    }
}
